package com.xwli.business.test;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @date: 2022/6/2 9:29
 * @author: xwli
 * @desc: 日期工具
 * @project: springcloud
 * @since: JDK 1.8
 */
public class DateFormatUtils {

    public static final Calendar CALENDAR = Calendar.getInstance();

    public static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static void main(String[] args) {
        System.out.println(getMinAfterDate(new Date(),10));
        System.out.println(getHourAfterDate(new Date(),10));
        CALENDAR.set(2023,10,1,11,11,11); // 指定时间
        System.out.println(DATE_FORMAT.format(CALENDAR.getTime()));
    }

    /**
     * 获取指定分钟后的时间
     * @param date
     * @param num
     * @return
     */
    public static String getMinAfterDate(Date date, Integer num) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE,num);
        return DATE_FORMAT.format(calendar.getTime());
    }

    /**
     * 获取指定小时后的时间
     * @param date
     * @param num
     * @return
     */
    public static String getHourAfterDate(Date date, Integer num) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR,num);
        return DATE_FORMAT.format(calendar.getTime());
    }
}

